/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package news;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class News {

    public static String getRandomString(int length, String type) {
        String allStr = getString(type);
        String[] arrStr = allStr.split(splitStr);
        StringBuffer pstr = new StringBuffer();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                pstr.append(arrStr[new Random().nextInt(arrStr.length)]);
            }
        }
        return pstr.toString();
    }
    private static final String splitStr = " "; // 分割符  

    // 取数字字符串 用 splitStr 分割  
    private static String getNumberString() {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 10; i++) {
            buf.append(String.valueOf(i));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 取大写字母字符串 用 splitStr 分割  
    private static String getUppercase() {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 26; i++) {
            buf.append(String.valueOf((char) ('A' + i)));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 取小写字母字符串 用 splitStr 分割  
    private static String getLowercase() {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 26; i++) {
            buf.append(String.valueOf((char) ('a' + i)));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 取特殊字符串 用 splitStr 分割  
    private static String getSpecialString() {
        String str = "~@#$%^&*()_+|\\=-`";
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            buf.append(str.substring(i, i + 1));
            buf.append(splitStr);
        }
        return buf.toString();
    }

    // 根据所取的字符串类型连接相应的字符串并返回  
    private static String getString(String type) {
        StringBuffer pstr = new StringBuffer();
        if (type.length() > 0) {
            if (type.indexOf('i') != -1) {
                pstr.append(getNumberString());
            }
            if (type.indexOf('l') != -1) {
                pstr.append(getLowercase());
            }
            if (type.indexOf('u') != -1) {
                pstr.append(getUppercase());
            }
            if (type.indexOf('s') != -1) {
                pstr.append(getSpecialString());
            }

        }
        return pstr.toString();
    }

    public static long getSecond(String time) throws ParseException {

        int second = 0;


        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm");
        Date date = format.parse(time);
        long ss = date.getTime();
        //System.out.println(ss);
        return ss;
    }
    News news = new News();

    public static void main(String arge[]) {
        long lasting = System.currentTimeMillis();
        String Sql = "";
        System.out.println(News.getRandomString(16, "ilu"));

        try {
            File f = new File("news.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(f);
            NodeList nl = doc.getElementsByTagName("topic");
            // String sql_insert = "";
            String template = "INSERT INTO supe_spaceitems(itemid, catid,uid,tid,username,itemtypeid,type,subtype,subject,dateline,lastpost, "
                    + "viewnum,replynum,digest,top,allowreply,hash,haveattach,grade,gid,gdigest,password,styletitle,picid,fromtype,fromid) VALUES ('Wilson', 'Champs-Elysees')";
          System.out.println(template);
            for (int i = 0; i < nl.getLength(); i++) {
                //String temp = "insert into supe_spaceitems ()";
                String ff = "(#itemid#,#catid#,#uid#,0,'shangxian',0,'news','','#subject#',#dateline#,#lastpost#, "
                        + "400,0,0,0,1,'#hash#',0,0,0,0,0,'',0,'adminpost',0)";
                String temStr = "(";

                ff = ff.replaceAll("#hash#", News.getRandomString(16, "ilu"));
                if (doc.getElementsByTagName("tid").item(i).getFirstChild() != null) {
                    ff = ff.replaceAll("#itemid#", doc.getElementsByTagName("tid").item(i).getFirstChild().getNodeValue());
                    temStr = temStr + doc.getElementsByTagName("tid").item(i).getFirstChild().getNodeValue() + ",";
                } else {
                    ff = ff.replaceAll("#itemid#", "1");
                }
                if (doc.getElementsByTagName("title").item(i).getFirstChild() != null) {
                    ff = ff.replaceAll("#subject#", doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue());
                    temStr = temStr + doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue() + ",";
                } else {
                    ff = ff.replaceAll("#subject#", "'尚弦'");
                }
                if (doc.getElementsByTagName("publishTime").item(i).getFirstChild() != null) {
                    String t = "" + News.getSecond(doc.getElementsByTagName("publishTime").item(i).getFirstChild().getNodeValue())/10000;
                    ff = ff.replaceAll("#dateline#", t);
                    ff = ff.replaceAll("#lastpost#", t);

                } else {
                    ff = ff.replaceAll("#subject#", "132757134");
                    ff = ff.replaceAll("#dateline#", "132757134");
                    ff = ff.replaceAll("#lastpost#", doc.getElementsByTagName("publishTime").item(i).getFirstChild().getNodeValue());
                    temStr += "132757134" + ",";
                }
                if (doc.getElementsByTagName("url").item(i).getFirstChild() != null) {
                    temStr = temStr + doc.getElementsByTagName("url").item(i).getFirstChild().getNodeValue() + ",";

                }
                if (doc.getElementsByTagName("publisher").item(i).getFirstChild() != null) {
                    switch(doc.getElementsByTagName("publisher").item(i).getFirstChild().getNodeValue()){
                        case "olyjane":
                            ff = ff.replaceAll("#uid#","166");
                        case "eshangxian":
                            ff = ff.replaceAll("#uid#","156");
                        case "icywrw":
                            ff = ff.replaceAll("#uid#","34");
                        case "qijiyifan":
                            ff = ff.replaceAll("#uid#","88");
                        case "Lauren":
                            ff = ff.replaceAll("#uid#","7");
                        case "jwyt":
                            ff = ff.replaceAll("#uid#","221");
                        case "admin":
                            ff = ff.replaceAll("#uid#","1");
                        case "admin_supesite":
                            ff = ff.replaceAll("#uid#","222");
                        default:
                             ff = ff.replaceAll("#uid#","222");
                    }
                    //ff = ff.replaceAll("#uid#", doc.getElementsByTagName("publisher").item(i).getFirstChild().getNodeValue());
                    temStr = temStr + doc.getElementsByTagName("publisher").item(i).getFirstChild().getNodeValue() + ",";
                }
                else {
                    ff = ff.replaceAll("#uid#","156");
                }
                if (doc.getElementsByTagName("systemType").item(i).getFirstChild() != null) {
                    //ff = ff.replaceAll("#catid#", doc.getElementsByTagName("systemType").item(i).getFirstChild().getNodeValue());
                    String test = doc.getElementsByTagName("systemType").item(i).getFirstChild().getNodeValue();
                    if (test.equals("尚弦纪事")||test=="尚弦纪事") {
                        ff=ff.replaceAll("#catid#", "52");
                    } else if (test.equals("1月纪事")) {
                       ff= ff.replaceAll("#catid#", "54");
                    } else if (test.equals("2月纪事")) {
                        ff=ff.replaceAll("#catid#", "55");
                    } else if (test.equals("3月纪事")) {
                        ff=ff.replaceAll("#catid#", "56");
                    } else if (test.equals("4月纪事")) {
                        ff=ff.replaceAll("#catid#", "57");
                    } else if (test.equals("5月纪事")) {
                        ff=ff.replaceAll("#catid#", "58");
                    } else if (test.equals("6月纪事")) {
                        ff=ff.replaceAll("#catid#", "59");
                    } else if (test.equals("7月纪事")) {
                        ff=ff.replaceAll("#catid#", "60");
                    } else if (test.equals("8月纪事")) {
                        ff=ff.replaceAll("#catid#", "61");
                    } else if (test.equals("9月纪事")) {
                        ff=ff.replaceAll("#catid#", "62");
                    } else if (test.equals("10月纪事")) {
                        ff=ff.replaceAll("#catid#", "63");
                    } else if (test.equals("11月纪事")) {
                        ff=ff.replaceAll("#catid#", "64");
                    } else if (test.equals("12月纪事")) {
                        ff.replaceAll("#catid#", "65");
                    } else if (test.equals("乐坛逸事")) {
                        ff=ff.replaceAll("#catid#", "51");
                    } else if (test.equals("尚弦资讯")) {
                        ff=ff.replaceAll("#catid#", "53");
                    } else if (test.equals("乐界动态")) {
                        ff=ff.replaceAll("#catid#", "66");
                    } else if (test.equals("比赛信息")) {
                        ff=ff.replaceAll("#catid#", "67");
                    } else if (test.equals("厂商动态")) {
                        ff=ff.replaceAll("#catid#", "68");
                    } else if (test.equals("尚弦数据")) {
                        ff=ff.replaceAll("#catid#", "69");
                    } else if (test.equals("人物访谈")) {
                        ff=ff.replaceAll("#catid#", "70");
                    } else if (test.equals("乐界评论")) {
                        ff=ff.replaceAll("#catid#", "71");
                    } else if (test.equals("演出购票")) {
                        ff=ff.replaceAll("#catid#", "72");
                    } else {
                        ff=ff.replaceAll("#catid#", "900");
                    }
                    ff=ff.replaceAll("#catid#", "900");
                }
                if (doc.getElementsByTagName("from").item(i).getFirstChild() != null) {
                    temStr = temStr + doc.getElementsByTagName("from").item(i).getFirstChild().getNodeValue() + ",";
                }

                if (doc.getElementsByTagName("tag").item(i).getFirstChild() != null) {
                    temStr = temStr + doc.getElementsByTagName("tag").item(i).getFirstChild().getNodeValue() + ",";
                }
                if (doc.getElementsByTagName("content").item(i).getFirstChild() != null) {
                    temStr = temStr + doc.getElementsByTagName("content").item(i).getFirstChild().getNodeValue() + ",";
                }
                if (doc.getElementsByTagName("image").item(i).getFirstChild() != null) {
                    temStr = temStr + doc.getElementsByTagName("image").item(i).getFirstChild().getNodeValue();
                }

                System.out.println(ff+",");

                ff = ff + ',';
                Sql += ff;
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("运行时间：" + (System.currentTimeMillis() - lasting) + " 毫秒");
        }

        //System.out.println(Sql);

        System.out.println(
                "运行时间：" + (System.currentTimeMillis() - lasting) + " 毫秒");
    }
}
